﻿#include "C3fdAnalysisTree.h"
#include <QMenu>
#include <QAction>
#include <QContextMenuEvent>
#include <QCursor>
#include <QRect>
#include <QMessageBox>

C3fdAnalysisTree::C3fdAnalysisTree(QWidget *parent) : QTreeWidget(parent)
{
	setFocusPolicy(Qt::NoFocus);
	setContextMenuPolicy(Qt::DefaultContextMenu);
}

void C3fdAnalysisTree::addSlot()
{
	m_Action = "add";
	m_CurrentItem = new QTreeWidgetItem(this);
	m_CurrentItem->setText(0, tr("Untitled"));
	m_CurrentItem->setIcon(0, QIcon(":/QUI/icon/c3fd_wc.png"));
	this->addTopLevelItem(m_CurrentItem);

	if (m_NameEditor == nullptr)
	{
		m_NameEditor = new C3fdLineEdit(this);
		m_NameEditor->setStyleSheet("background:white;");
		connect(m_NameEditor, SIGNAL(returnPressed()), this, SLOT(finishRenameSlot()));
	}
	m_NameEditor->raise();
	m_NameEditor->setText(m_CurrentItem->text(0));
	auto rect = visualItemRect(m_CurrentItem);
	m_NameEditor->setGeometry(rect.left() + 32, rect.top() + 30, rect.width() - 32, rect.height());
	m_NameEditor->show();
	m_NameEditor->selectAll();
	m_NameEditor->setFocus();
}

void C3fdAnalysisTree::renameSlot()
{
	if (m_CurrentItem == nullptr)
	{
		return;
	}
	m_Action = "rename";
	if (m_NameEditor == nullptr)
	{
		m_NameEditor = new C3fdLineEdit(this);
		m_NameEditor->setStyleSheet("background:white;");
		connect(m_NameEditor, SIGNAL(returnPressed()), this, SLOT(finishRenameSlot()));
	}
	m_NameEditor->raise();
	m_NameEditor->setText(m_CurrentItem->text(0));
	auto rect = visualItemRect(m_CurrentItem);
	m_NameEditor->setGeometry(rect.left() + 32, rect.top() + 30, rect.width() - 32, rect.height());
	m_NameEditor->show();
	m_NameEditor->selectAll();
	m_NameEditor->setFocus();
}

void C3fdAnalysisTree::deleteSlot()
{
	if (m_CurrentItem == nullptr)
	{
		return;
	}
	auto name = m_CurrentItem->text(0);
	emit itemNameChanged(m_CurrentItem, name, name, "delete");
}

void C3fdAnalysisTree::finishRenameSlot()
{
	if (m_NameEditor == nullptr || m_CurrentItem == nullptr)
	{
		return;
	}

	QString oldName = "";

	if (m_Action == "rename") {
		oldName = m_CurrentItem->text(0);
	}

	auto newName = m_NameEditor->text();

	if (newName != oldName)
	{
		if (hasSameNameItemInThisLevel(newName, m_CurrentItem))
		{
			QMessageBox::critical(this, tr("Error Info"), tr("Same name node has exist in the same level of tree!"));
			return;
		}
		m_CurrentItem->setText(0, newName);
		if (!m_Action.isEmpty()) {
			emit itemNameChanged(m_CurrentItem, oldName, newName, m_Action);
		}
	}

	// m_NameEditor->setText("");
	m_NameEditor->hide();
	m_CurrentItem = nullptr;
}

void C3fdAnalysisTree::contextMenuEvent(QContextMenuEvent *event)
{
	m_CurrentItem = itemAt(event->pos());
	if (m_CurrentItem == nullptr)
	{
		QMenu *menu = new QMenu(this);
		QAction *addAction = new QAction(tr("Add"), menu);
		connect(addAction, SIGNAL(triggered()), this, SLOT(addSlot()));
		menu->addAction(addAction);
		menu->exec(QCursor::pos());
	}
	else if (indexOfTopLevelItem(m_CurrentItem) >= 0)
	{
		QMenu *menu = new QMenu(this);
		QAction *renameAction = new QAction(tr("Rename"), menu);
		connect(renameAction, SIGNAL(triggered()), this, SLOT(renameSlot()));
		menu->addAction(renameAction);
		QAction *deleteAction = new QAction(tr("Delete"), menu);
		connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteSlot()));
		menu->addAction(deleteAction);
		menu->exec(QCursor::pos());
	}
	else {
		m_CurrentItem = nullptr;
	}


}

bool C3fdAnalysisTree::hasSameNameItemInThisLevel(QString name, QTreeWidgetItem *item) const
{
	if (name.isEmpty() || item == nullptr)
	{
		return false;
	}
	auto *parentItem = item->parent();

	if (parentItem == nullptr)
	{
		for (int i = topLevelItemCount() - 1; i >= 0; --i)
		{
			if (topLevelItem(i)->text(0) == name)
			{
				return true;
			}
		}
		return false;
	}

	for (int i = parentItem->childCount() - 1; i >= 0; --i)
	{
		if (parentItem->child(i)->text(0) == name)
		{
			return true;
		}
	}
	return false;
}
